<template>
    <div>
        <el-row class="title">
            <h3>合作企业</h3>
        </el-row>
        <el-row>
            <el-form :inline="true" @submit.native.prevent>
                <el-form-item>
                    <el-button type="primary" @click="showDialog"><i class="el-icon-plus el-icon--left"></i>添加合作企业</el-button>
                </el-form-item>
                <el-form-item label="合作企业">
                    <el-input class="search-input" v-model="enterpriseNameLike" placeholder="请输入合作企业名称" clearable @change="getData"></el-input>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row>
            <el-table :data="dataList" border>
                <el-table-column prop="enterpriseId" label="id" align="center"></el-table-column>
                <el-table-column prop="enterpriseName" label="名称" align="center"></el-table-column>
                <el-table-column prop="enterpriseRemark" label="描述" align="center"></el-table-column>
                <el-table-column prop="orderNumber" label="排序值" align="center"></el-table-column>
                <el-table-column label="图片" align="center" width="200px">
                    <template slot-scope="scope">
                        <img :src="UPLOADURL + scope.row.enterpriseLogoUrl" alt="" class="poster">
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="160px">
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" @click="updateDatas(scope.$index, scope.row)">修改</el-button>
                        <el-button size="small" type="danger" @click="deleteDatas(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
        <el-dialog :title="add ? '添加' : '修改' + '合作企业'" :visible.sync="dialogShow" width="400px" @close="closeDialog">
            <el-form label-width="120px">
                <el-form-item label="名称">
                    <el-input v-model="cooperation.enterpriseName" placeholder="名称"></el-input>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input v-model="cooperation.enterpriseRemark" placeholder="描述"></el-input>
                </el-form-item>
                <el-form-item label="排序值">
                    <el-input-number v-model="cooperation.orderNumber" :min="1" label="排序值"></el-input-number>
                </el-form-item>
                <el-form-item label="企业图片">
                    <el-upload :headers="uploadHeader" :action="BASEURL + '/upload/single/poster'" :show-file-list="false" :on-success="uploadPosterSuccess">
                        <el-button size="small" type="primary">上传图片</el-button>
                    </el-upload>
                    <div v-if="imageUrl">
                        <img :src="UPLOADURL + imageUrl" alt="" class="poster-thumb">
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="save" :loading="loading">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 分页 -->
        <el-row>
            <el-col class="pagination">
                <el-pagination @current-change="currentChange" :current-page="pageId" :page-size="pageSize" layout="total, prev, pager, next" :total="count">
                </el-pagination>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import {
    getCooperationLists,
    addCooperation,
    deleteCooperationById,
    updateCooperationById
} from '@/api/api'
export default {
    data: function() {
        return {
            dataList: [],
            add: true,
            dialogShow: false,
            loading: false,
            pageId: 1,
            pageSize: 10,
            count: 0,
            enterpriseNameLike: '',
            cooperation: {
                enterpriseId: null,
                enterpriseName: '',
                enterpriseLogoUrl: '',
                enterpriseRemark: '',
                orderNumber: 1
            },
            imageUrl: '',
            uploadHeader: {
                TOKEN: sessionStorage.getItem('jwt')
            }
        }
    },
    created: function() {
        this.pageId = parseInt(this.$route.query.page) || 1;
        this.getData();
    },
    methods: {
        getData: function() {
            getCooperationLists({
                params: {
                    pageId: this.pageId,
                    pageSize: this.pageSize,
                    enterpriseNameLike: this.enterpriseNameLike
                }
            }).then(res => {
                console.log(res)
                this.dataList = res.list;
                this.count = res.count;
            })
        },
        showDialog: function() {
            this.add = true;
            this.dialogShow = true;
        },
        closeDialog: function() {
            this.dialogShow = false;
            this.add = false;
            this.cooperation = {
                enterpriseId: null,
                enterpriseName: '',
                enterpriseLogoUrl: '',
                enterpriseRemark: '',
                orderNumber: 1
            }
            this.imageUrl = '';
        },
        save: function() {
            if (this.add) {
                addCooperation(this.cooperation).then(res => {
                    this.getData();
                    this.$message({
                        message: '添加成功!',
                        duration: 2000,
                        type: 'success'
                    });
                    this.closeDialog();
                })
            } else {
                updateCooperationById(this.cooperation.enterpriseId, this.cooperation).then(res => {
                    this.getData();
                    this.$message({
                        message: '修改成功!',
                        duration: 2000,
                        type: 'success'
                    });
                    this.closeDialog();
                })
            }
        },
        updateDatas: function(index, row) {
            this.cooperation = {
                enterpriseId: row.enterpriseId,
                enterpriseName: row.enterpriseName,
                enterpriseLogoUrl: row.enterpriseLogoUrl,
                enterpriseRemark: row.enterpriseRemark,
                orderNumber: row.orderNumber
            }
            this.add = false;
            this.imageUrl = row.enterpriseLogoUrl;
            this.dialogShow = true;
        },
        deleteDatas: function(index, row) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteCooperationById(row.enterpriseId).then(res => {
                    this.getData();
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        uploadPosterSuccess: function(res) {
            if (res.status) {
                this.$message({
                    message: '上传成功',
                    type: 'success'
                })
                this.imageUrl = res.data.webPath;
                this.cooperation.enterpriseLogoUrl = res.data.webPath;
            } else {
                this.$message({
                    message: '上传失败',
                    type: 'error'
                })
            }
        },
        currentChange: function(val) {
            this.$router.push('?page=' + val);
            this.pageId = val;
            this.getData();
        }
    }
}
</script>
<style scoped lang="scss">
.title {
    font-size: 12px;
    margin-bottom: 5px;
    h3 {
        display: inline-block;
        padding: 5px 0;
        margin: 0;
        font-weight: normal;
        color: #000;
        border-bottom: 2px solid #000;
    }
}
.poster {
    max-width: 100%;
}

.poster-thumb {
    max-width: 100%;
}
</style>